//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UnitOfMeasure.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Math/Rounding.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (String^ name, String^ code, QL::Experimental::Commodities::UnitOfMeasure::TypeEnum unitType) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phUnitOfMeasure = NULL;
#endif
        std::string _name = (std::string)ValueHelper::Convert (name); //d
        std::string _code = (std::string)ValueHelper::Convert (code); //d
        QuantLib::UnitOfMeasure::Type _unitType = (QuantLib::UnitOfMeasure::Type)unitType ;
        _ppUnitOfMeasure = new boost::shared_ptr<QuantLib::UnitOfMeasure> (new QuantLib::UnitOfMeasure ( _name,  _code,  _unitType ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phUnitOfMeasure = NULL;
#endif
        _ppUnitOfMeasure = new boost::shared_ptr<QuantLib::UnitOfMeasure> (new QuantLib::UnitOfMeasure ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (boost::shared_ptr<QuantLib::UnitOfMeasure>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasure = NULL;
#endif
	_ppUnitOfMeasure = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (QuantLib::UnitOfMeasure& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phUnitOfMeasure = NULL;
#endif
	_ppUnitOfMeasure = new boost::shared_ptr<QuantLib::UnitOfMeasure> (&childNative);
    
    _UnitOfMeasureOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (CUnitOfMeasure^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUnitOfMeasure = new boost::shared_ptr<QuantLib::UnitOfMeasure> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasure = NULL;
#endif
	if (!t->IsSubclassOf(CUnitOfMeasure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (QuantLib::Handle<QuantLib::UnitOfMeasure>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasure = &childNative;
	_ppUnitOfMeasure = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasure>>(childNative.currentLink());
    
    _UnitOfMeasureOwner = owner;
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (QuantLib::Handle<QuantLib::UnitOfMeasure> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasure = &childNative;
	_ppUnitOfMeasure = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasure>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::CUnitOfMeasure (QuantLib::UnitOfMeasure childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasure = NULL;
#endif
	_ppUnitOfMeasure = new boost::shared_ptr<QuantLib::UnitOfMeasure> (new QuantLib::UnitOfMeasure (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CUnitOfMeasure::~CUnitOfMeasure ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppUnitOfMeasure != NULL)
    {
	    delete _ppUnitOfMeasure;
        _ppUnitOfMeasure = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasure::!CUnitOfMeasure ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppUnitOfMeasure != NULL)
    {
	    delete _ppUnitOfMeasure;
    }
}
QuantLib::UnitOfMeasure& Cephei::QL::Experimental::Commodities::CUnitOfMeasure::GetReference ()
{
    if (_ppUnitOfMeasure == NULL) throw REFNEW NativeNullException ();
	return **_ppUnitOfMeasure;
}
boost::shared_ptr<QuantLib::UnitOfMeasure>& Cephei::QL::Experimental::Commodities::CUnitOfMeasure::GetShared ()
{
    if (_ppUnitOfMeasure == NULL) throw REFNEW NativeNullException ();
	return *_ppUnitOfMeasure;
}
QuantLib::UnitOfMeasure* Cephei::QL::Experimental::Commodities::CUnitOfMeasure::GetPointer ()
{
    if (_ppUnitOfMeasure == NULL) throw REFNEW NativeNullException ();
	return &**_ppUnitOfMeasure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UnitOfMeasure>& Cephei::QL::Experimental::Commodities::CUnitOfMeasure::GetHandle ()
{
	if (_phUnitOfMeasure == NULL)
	{
		_phUnitOfMeasure = new Handle<QuantLib::UnitOfMeasure> (*_ppUnitOfMeasure);
	}
	return *_phUnitOfMeasure;
}
#endif
bool Cephei::QL::Experimental::Commodities::CUnitOfMeasure::HasNative () 
{
	return (_ppUnitOfMeasure != NULL);
}

String^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure::Code::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppUnitOfMeasure)->code ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CUnitOfMeasure::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppUnitOfMeasure)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppUnitOfMeasure)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IRounding^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure::Rounding::get ()
{
    try
    {
    	QuantLib::Rounding& _rv = (QuantLib::Rounding&)(*_ppUnitOfMeasure)->rounding ( );   
        Cephei::QL::Math::CRounding^ _nrv = REFNEW Cephei::QL::Math::CRounding (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure::TriangulationUnitOfMeasure::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppUnitOfMeasure)->triangulationUnitOfMeasure ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Experimental::Commodities::UnitOfMeasure::TypeEnum Cephei::QL::Experimental::Commodities::CUnitOfMeasure::UnitType::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure::Type _rv = (QuantLib::UnitOfMeasure::Type)(*_ppUnitOfMeasure)->unitType ( );   
        QL::Experimental::Commodities::UnitOfMeasure::TypeEnum _nrv = (QL::Experimental::Commodities::UnitOfMeasure::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure_Factory::Create (String^ name, String^ code, QL::Experimental::Commodities::UnitOfMeasure::TypeEnum unitType)
{
    return REFNEW CUnitOfMeasure ( name,  code,  unitType);
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CUnitOfMeasure_Factory::Create ()
{
    return REFNEW CUnitOfMeasure ();
}
